package com.cgsd.heyu.ui.info.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.adapter.BaseAdapter;
import com.android.library.adapter.ViewHolder;
import com.android.library.utils.BeanUtils;
import com.android.library.utils.TimeUtil;
import com.cgsd.heyu.R;
import com.cgsd.heyu.entity.AbonusRecordEntity;
import com.cgsd.heyu.entity.IntegralEntity;
import com.cgsd.heyu.http.ErrorMessage;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.presenter.home.HomePresenter;
import com.cgsd.heyu.ui.BaseFragment;
import com.cgsd.heyu.widgets.ErrorCustomView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 分红明细
 * CreateData: 2021/1/30
 */
public class AbonusRecordFragment extends BaseFragment implements HomeContract.AbonusRecordView, OnLoadMoreListener {
    //全部
    public static final String TYPE_ALL = "-1";
    //未结束
    public static final String TYPE_INCOME = "1";
    //已结算
    public static final String TYPE_EXPEND = "2";

    private BaseAdapter<AbonusRecordEntity> adapter;
    private List<AbonusRecordEntity> data = new ArrayList<AbonusRecordEntity>();
    private String type;
    private HomeContract.Presenter mPresenter;
    private SmartRefreshLayout mSmartRefreshLayout;
    private ErrorCustomView mMErrorView;


    public static AbonusRecordFragment newInstance(String type){
        AbonusRecordFragment fragment = new AbonusRecordFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type",type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null){
            type = getArguments().getString("type");
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_abonus_record;
    }

    @Override
    protected void initView() {
        new HomePresenter(this);
        mSmartRefreshLayout = findViewById(R.id.mSmartRefreshLayout);
        mSmartRefreshLayout.setEnableRefresh(false);
        mMErrorView = findViewById(R.id.mErrorView);

        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new BaseAdapter<AbonusRecordEntity>(mContext,R.layout.item_abonus_record,data) {
            @SuppressLint("SetTextI18n")
            @Override
            public void convert(ViewHolder holder, AbonusRecordEntity abonusRecordEntity, int position) {
                TextView tvIntegral = holder.getView(R.id.tv_price);
                holder.setText(R.id.tv_title,abonusRecordEntity.getFlowTitle());
                holder.setText(R.id.tv_des,"备注： "+abonusRecordEntity.getRemark());
                holder.setText(R.id.tv_time, TimeUtil.dateString(abonusRecordEntity.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
                if (TYPE_INCOME.equals(abonusRecordEntity.getOperationType())){
                    tvIntegral.setTextColor(Color.parseColor("#E94E4E"));
                    tvIntegral.setText(abonusRecordEntity.getAmount());
                }else {
                    tvIntegral.setTextColor(Color.parseColor("#00BE9A"));
                    tvIntegral.setText(abonusRecordEntity.getAmount());
                }
            }
        };
        mRecyclerView.setAdapter(adapter);

        mSmartRefreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    protected void initData() {
        mPresenter.getAbonusRecord(page,type);
    }

    @Override
    public void setAbonusRecordData(List<AbonusRecordEntity> entities) {
        mSmartRefreshLayout.finishLoadMore();
        mSmartRefreshLayout.setEnableLoadMore(false);
        if (BeanUtils.isNotEmpty(entities)){
            int index = data.size();
            data.addAll(entities);
            adapter.notifyItemRangeChanged(index,entities.size());
            if (entities.size() == 20) {
                mSmartRefreshLayout.setEnableLoadMore(true);
            }
        }
        if (data.size() == 0){
            showErrorView();
        }else {
            mMErrorView.setVisibility(View.GONE);
        }
    }

    @Override
    public void setPresenter(HomeContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void setErrorMessage(ErrorMessage error) {
        handleErrorAction(error);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        mPresenter.getAbonusRecord(page,type);
    }

    /**
     * 数据为null
     */
    private void showErrorView(){
        mMErrorView.showNetworkError(R.mipmap.default_no_data,"暂无分红～");
        mMErrorView.setVisibility(View.VISIBLE);
    }
}
